<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="form.userName" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <!-- <el-form-item label="证件类型">
        <el-select v-model="form.IdCardType" placeholder="证件类型">
          <el-option v-for=" item in options " :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="证件号码">
        <el-input v-model="form.IdCardNumber" />
      </el-form-item> -->
      <!-- <el-form-item label="角色">
        <el-input v-model="form.Roles" />
      </el-form-item> -->
      <div class="form-footer">
        <el-button :loading="loading" type="primary" class="btn" @click.native.prevent="onSubmit('form')"
          >提交</el-button
        >
        <el-button type="secondary" class="btn" @click.native.prevent="onCancel">取消</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { ID_CARD_TYPE } from '@common/consts/user';
// 手机号验证
const validatePhone = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入手机号'));
  } else {
    if (!/^1[3456789]\d{9}$/.test(value)) {
      callback(new Error('请输入正确的手机号'));
    } else {
      callback();
    }
  }
};

export default {
  props: {
    defaultFormData: {
      type: Object,
      default() {
        return {};
      },
    },
    loading: {
      type: Boolean,
      default() {
        false;
      },
    },
  },
  data() {
    return {
      options: ID_CARD_TYPE,
      form: {
        userName: '',
        mobile: '',
        // idCardNumber: '',
        // idCardType: ID_CARD_TYPE[0].value,
      },
      rules: {
        userName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        mobile: [
          // { required: true, message: '请输入正确的手机号', trigger: 'blur' },
          { required: true, validator: validatePhone, trigger: 'blur' },
        ],
      },
    };
  },
  watch: {
    defaultFormData: {
      immediate: true,
      handler() {
        this.form = { ...this.defaultFormData };
      },
    },
  },
  methods: {
    onSubmit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          this.$emit('form-submit', this.form);
        }
        return false;
      });
    },
    onCancel() {
      this.$emit('form-hide');
    },
  },
};
</script>

<style scoped>
.form-footer {
  margin: 0 auto;
  text-align: center;
}
.line {
  text-align: center;
}
</style>

